<?php

namespace App\Admin\Extensions\Table;

use App\Admin\Repositories\User;
use App\Models\Area;
use App\Models\Partition;
use App\Models\School;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
use Dcat\Admin\Widgets\DialogTable;

class ShareUserTable extends LazyRenderable
{
    public function grid(): Grid
    {
        // 获取外部传递的参数
        $user_id = $this->payload['user_id'];
        admin_style(".fa.fa-clone{ display:none; }");
        return Grid::make(new User(), function (Grid $grid) use($user_id) {
            $grid->model()->with(['partition','area','school'])
                ->withCount(["posts","follow","fans","donate"])
//                ->whereNotNull("mobile")
                ->where("tid",$user_id)
                ->orderByDesc("id");
            $grid->disableViewButton();
            $grid->disableQuickEditButton();
            $grid->column('id')->sortable();
            $grid->column('avatar','微信头像[昵称]')->image('',50,50)->append(function () {
                $nickname = $this->nickname;
                return "[{$nickname}]";
            });
//            $grid->column('nickname');
            $grid->column('name','姓名');
            $grid->column('mobile','手机号');
            $grid->column('partition.name','分区/地区/高校')->append(function ($value) {
                $names = [];
                if (isset($this->area->name)){
                    $names[] = $this->area->name;
                }
                if (isset($this->school->name)){
                    $names[] = $this->school->name;
                }
                return $value?"/".implode("/",$names):"";
            })->limit(15);
//            $grid->column('area.name','所在地区');
//            $grid->column('school.name','所在高校');
            $grid->column('faculty','院系');
            $grid->column('student_no','学号');
            $grid->column('pioneer_score','创业币')->sortable();
            $grid->column('love_score','爱心值')->sortable();
            $grid->column('follow_count','关注数')->label('info')->modal(function ($modal) {
                $modal->title("查看【用户:{$this->nickname}】的关注");
                $modal->xl();
                return UserTable::make(['user_id' => $this->id,'way'=>0]);
            })->sortable();
            $grid->column('fans_count','粉丝数')->label('info')->modal(function ($modal) {
                $modal->title("查看【用户:{$this->nickname}】的粉丝");
                $modal->xl();
                return UserTable::make(['user_id' => $this->id,'way'=>1]);
            })->sortable();
            $grid->column('posts_count','发帖数量')->label('info')->modal(function ($modal) {
                $modal->title("查看【用户:{$this->nickname}】的帖子");
                $modal->xl();
                return PostsTable::make(['user_id' => $this->id]);
            })->sortable();
            $grid->column('donate_count','捐赠数量')->label('info')->modal(function ($modal) {
                $modal->title("查看【用户:{$this->nickname}】的捐赠");
                $modal->xl();
                return DonateTable::make(['user_id' => $this->id]);
            })->sortable();
            $grid->column('status')->using(['启用','禁用'])->label();
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['id','name','nickname','mobile']);
            $grid->filter(function (Grid\Filter $filter){
                $filter->expand(false);
                $filter->panel();
                $filter->equal("id")->width(3);
                $filter->like("nickname")->width(3);
                $filter->like("name")->width(3);
                $filter->like("mobile")->width(3);
                $filter->equal("partition_id","所在分区")->select(Partition::getIds())->width(3);
                $filter->equal("area_id","所在地区")->select(Area::getIds())->width(3);
                $filter->equal("school_id","所在高校")->select(School::getIds())->width(3);
                $filter->like("faculty","院系")->width(3);
                $filter->like("student_no","学号")->width(3);
            });
            $grid->rowSelector()->disable(function ($row) {
                return $row->id === 1;
            });
        });
    }


}
